package pe.com.lgutierrez.campeonato.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.stereotype.Component;

@Component
public class ProcesarArchivoExcel {

    public List<Object> leerExcel(InputStream archivoEnviado) {
        /**
         * Create a new instance for cellDataList
         */
        List<Object> cellDataList = new ArrayList<Object>();
        try{
        /**
         * Create a new instance for POIFSFileSystem class
         */
        POIFSFileSystem fsFileSystem = new POIFSFileSystem(archivoEnviado);
        /*
         * Create a new instance for HSSFWorkBook Class
         */
        HSSFWorkbook workBook = new HSSFWorkbook(fsFileSystem);
        int numeroHojas = workBook.getNumberOfSheets();
        for (int j = 0; j < numeroHojas; j++) {
            HSSFSheet hssfSheet = workBook.getSheetAt(j);
            Iterator rowIterator = hssfSheet.rowIterator();
            HSSFRow hssfRow = (HSSFRow) rowIterator.next();
            while (rowIterator.hasNext()) {
                hssfRow = (HSSFRow) rowIterator.next();
                Iterator iterator = hssfRow.cellIterator();
                List<Object> cellTempList = new ArrayList<Object>();
                while (iterator.hasNext()) {
                    HSSFCell hssfCell = (HSSFCell) iterator.next();
                    cellTempList.add(hssfCell);
                }
                cellDataList.add(cellTempList);
            }
            
            
        }  
         }catch(FileNotFoundException fileNotFoundException){
             fileNotFoundException.printStackTrace();
             return null;
         }catch(IOException exception){
             exception.printStackTrace();
             return null;
         }
        
        return cellDataList;
    }
    
    public List<Object> leerExcel2007(InputStream archivoEnviado) {
        /**
         * Create a new instance for cellDataList
         */
        List<Object> cellDataList = new ArrayList<Object>();
        try{
        /**
         * Create a new instance for POIFSFileSystem class
         */
        POIFSFileSystem fsFileSystem = new POIFSFileSystem(archivoEnviado);
        /*
         * Create a new instance for HSSFWorkBook Class
         */
        HSSFWorkbook workBook = new HSSFWorkbook(fsFileSystem);
        int numeroHojas = workBook.getNumberOfSheets();
        for (int j = 0; j < numeroHojas; j++) {
            HSSFSheet hssfSheet = workBook.getSheetAt(j);
            Iterator rowIterator = hssfSheet.rowIterator();
            HSSFRow hssfRow = (HSSFRow) rowIterator.next();
            while (rowIterator.hasNext()) {
                hssfRow = (HSSFRow) rowIterator.next();
                Iterator iterator = hssfRow.cellIterator();
                List<Object> cellTempList = new ArrayList<Object>();
                while (iterator.hasNext()) {
                    HSSFCell hssfCell = (HSSFCell) iterator.next();
                    cellTempList.add(hssfCell);
                }
                cellDataList.add(cellTempList);
            }
            
            
        }  
         }catch(FileNotFoundException fileNotFoundException){
             fileNotFoundException.printStackTrace();
             return null;
         }catch(IOException exception){
             exception.printStackTrace();
             return null;
         }
        
        return cellDataList;
    }
}
